/*
  Copyright (c) 2014 Dimitrij Gester

  "Smart RC TX" is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  "Smart RC TX" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with "Smart RC TX"; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

package com.codeplex.smartrctx.configurator.main.model;

import java.util.Observable;

import org.simpleframework.xml.ElementArray;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.core.Commit;
import org.simpleframework.xml.core.Persist;

@Root
public class RCSettings extends Observable{
	public final static int CHANNEL_COUNT = 6; 
	
	@ElementArray
	public Integer[] preScaler = new Integer[CHANNEL_COUNT ];
	
	@ElementArray
	public Integer[] trimmer = new Integer[CHANNEL_COUNT];
	@ElementArray
	public Integer[] expo = new Integer[CHANNEL_COUNT];

	public Integer[][][] mixer = new Integer[2][CHANNEL_COUNT][CHANNEL_COUNT];
	
	public Integer[][] limiter = new Integer[2][CHANNEL_COUNT];

	@ElementArray
	private Integer[] mixerPersisted = new Integer[CHANNEL_COUNT * CHANNEL_COUNT * 2];
	
	@ElementArray
	private Integer[] limiterPersisted = new Integer[CHANNEL_COUNT * 2];
	
	@Persist
	public void preSave() {
		for(int i = 0; i < CHANNEL_COUNT; i++){
			// mixer
			for(int j = 0; j < CHANNEL_COUNT; j++) {
				mixerPersisted[i*CHANNEL_COUNT*2 + j*2 + 0] = mixer[0][j][i];
				mixerPersisted[i*CHANNEL_COUNT*2 + j*2 + 1] = mixer[1][j][i];
			}
			
			// limiter
			limiterPersisted[i*2] = limiter[0][i];
			limiterPersisted[i*2 + 1] = limiter[1][i];
		}
		
	}
	
	@Commit
	public void postLoad() {
		for(int i = 0; i < CHANNEL_COUNT; i++){
			for(int j = 0; j < CHANNEL_COUNT; j++) {
				mixer[0][j][i] = mixerPersisted[i*CHANNEL_COUNT*2 + j*2 + 0];
				mixer[1][j][i] = mixerPersisted[i*CHANNEL_COUNT*2 + j*2 + 1];
			}

			// limiter
			limiter[0][i] = limiterPersisted[i*2];
			limiter[1][i] = limiterPersisted[i*2 + 1];
		}

		settings.setChanged();
		settings.notifyObservers();

	}
	
	public static void newSettings() {
		getSettings();

		settings.preScaler = new Integer[CHANNEL_COUNT ];
		settings.trimmer = new Integer[CHANNEL_COUNT];
		settings.expo = new Integer[CHANNEL_COUNT];
		settings.mixer = new Integer[2][CHANNEL_COUNT][CHANNEL_COUNT];
		settings.limiter = new Integer[2][CHANNEL_COUNT];

		settings.setChanged();
		settings.notifyObservers();
}
	
	public static void initSettings(RCSettings s) {
		getSettings();
		settings.expo = s.expo;
		settings.mixer = s.mixer;
		settings.preScaler = s.preScaler;
		settings.trimmer = s.trimmer;
		settings.limiter = s.limiter;
		
		settings.setChanged();
		settings.notifyObservers();
	}
	
	static RCSettings settings;
	public static RCSettings getSettings() {
		if (settings == null) {
			settings = new RCSettings();
		}
		return settings;
	}
	
	
}
